package com.ecar.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/7/4.
 */
public class HttpClientUtil {

	private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);

	public static String httpGet(String url) {
		log.debug("http get:" + url);
		String ret = null;
		try (CloseableHttpClient client = HttpClients.createDefault()) {
			HttpGet method = new HttpGet(url);
			try (CloseableHttpResponse response = client.execute(method)) {
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
					ret = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.getMessage();
		}
		log.debug("结果：" + ret);
		return ret;
	}

	public static String httpPost(String url) {
		return httpPost(url, null);
		
	}

	public static String httpPost(String url, Map<String, String> params) {
		log.debug("http post:" + url);
		StringBuilder ret = new StringBuilder();

		try (CloseableHttpClient client = HttpClients.createDefault()) {
			HttpPost method = new HttpPost(url);
			if (params != null) {
				List<NameValuePair> list = new ArrayList<>();
				for (String key : params.keySet()) {
					list.add(new BasicNameValuePair(key, params.get(key)));
				}
				//fire
				//method.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				method.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
			}
			try (CloseableHttpResponse response = client.execute(method)) {
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					HttpEntity entity = response.getEntity();
					BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
					String line;
					while ((line = buf.readLine()) != null)
						ret.append(line);
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		log.debug("结果：" + ret.toString());
		return ret.toString();
	}

	public static String httpPostContent(String url, String body) {
		log.debug("http post:" + url);
		log.debug("http body:" + body);
		StringBuilder ret = new StringBuilder();

		try (CloseableHttpClient client = HttpClients.createDefault()) {
			HttpPost method = new HttpPost(url);
			if (StringUtil.isNotEmpty(body)) {
				StringEntity request = new StringEntity(body, "UTF-8");
				request.setContentType("application/json");
				method.setEntity(request);
				method.addHeader("Content-Type", "application/json;charset=UTF-8");
			}
			try (CloseableHttpResponse response = client.execute(method)) {
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					HttpEntity entity = response.getEntity();
					BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
					String line;
					while ((line = buf.readLine()) != null)
						ret.append(line);
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		log.info("结果：" + ret.toString());
		return ret.toString();
	}

	public static String httpPostContent(String url, String body, String charset, String contentType) {
		log.debug("http post:" + url);
		log.debug("http body:" + body);
		StringBuilder ret = new StringBuilder();

		org.apache.xmlbeans.impl.util.Base64.encode(body.getBytes());
		try (CloseableHttpClient client = HttpClients.createDefault()) {
			HttpPost method = new HttpPost(url);
			if (StringUtil.isNotEmpty(body)) {
				StringEntity request = new StringEntity(body, charset.toUpperCase());
				method.setEntity(request);
				method.addHeader("Content-Type", contentType+";charset="+charset);
			}
			try (CloseableHttpResponse response = client.execute(method)) {
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					HttpEntity entity = response.getEntity();
					BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
					String line;
					while ((line = buf.readLine()) != null)
						ret.append(line);
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		log.debug("结果：" + ret.toString());
		return ret.toString();
	}
}
